# -*- coding = utf-8 -*-
# @Time : 2023/12/14 13:27
# @Author : 龙王赘婿_彪少
# @File : plot.py
# @Software: PyCharm

import matplotlib
import numpy
import matplotlib.pyplot as plt
import openpyxl

class Mat:
    def __init__(self):
        print("matplotlib version = ", matplotlib.__version__)
        self.get_sheet()
        # self.get_value()
        self.get_value_2()
        # self.plot_speed()
        self.plot_speed_2()
        # self.plot_encoder()

    def get_sheet(self):
        # 读取数据
        self.book = openpyxl.load_workbook(r"E:/01 智能小车项目/07异常处理/20231213 右后轮去使能测试.xlsx")

        # sheet 获取
        # print(self.book.sheetnames)
        self.sheet1 = self.book["Sheet1"]
        self.sheet2 = self.book["Sheet2"]
        self.sheet3 = self.book["Sheet3"]
        self.sheet4 = self.book["Sheet4"]
        self.sheet5 = self.book["Sheet5"]
        self.sheet6 = self.book["Sheet6"]

    def get_value(self):
        self.sheet_now = self.sheet1
        self.speed_lf = []
        self.speed_rf = []
        self.speed_lb = []
        self.speed_rb = []
        self.encoder_lf = []
        self.encoder_rf = []
        self.encoder_lb = []
        self.encoder_rb = []

        for i in self.sheet_now["B"]:
            self.speed_lf.append(i.value)

        for i in self.sheet_now["D"]:
            self.speed_rf.append(i.value)

        for i in self.sheet_now["F"]:
            self.speed_lb.append(i.value)

        for i in self.sheet_now["H"]:
            self.speed_rb.append(i.value)

        for i in self.sheet_now["J"]:
            self.encoder_lf.append(i.value)

        for i in self.sheet_now["L"]:
            self.encoder_rf.append(i.value)

        for i in self.sheet_now["N"]:
            self.encoder_lb.append(i.value)

        for i in self.sheet_now["P"]:
            self.encoder_rb.append(i.value)

    def get_value_2(self):
        self.sheet_now = self.sheet1
        self.speed_lf = []
        self.speed_rf = []
        self.speed_lb = []
        self.speed_rb = []
        self.encoder_lf = []
        self.encoder_rf = []
        self.encoder_lb = []
        self.encoder_rb = []

        for i in self.sheet_now.iter_rows():
            self.speed_lf.append(i[1].value)    # 下标从0开始
            self.speed_rf.append(i[3].value)
            self.speed_lb.append(i[5].value)
            self.speed_rb.append(i[7].value)
            self.encoder_lf.append(i[9].value)
            self.encoder_rf.append(i[11].value)
            self.encoder_lb.append(i[13].value)
            self.encoder_rb.append(i[15].value)

    def plot_speed(self):
        y1 = numpy.array(self.speed_lf)
        y2 = numpy.array(self.speed_rf)
        y3 = numpy.array(self.speed_lb)
        y4 = numpy.array(self.speed_rb)
        plt.plot(y1, ":b", label=u"左前轮")
        plt.plot(y2, ":g", label=u"右前轮")
        plt.plot(y3, ":y", label=u"左后轮")
        plt.plot(y4, ":r", label=u"右后轮")
        plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
        plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

        plt.xlabel("时间", size=12)
        plt.ylabel("速度", size=12)

        plt.title("speed")
        plt.legend(shadow=True, fancybox="blue")

        plt.show()

    def plot_speed_2(self):
        x = numpy.arange(0, len(self.speed_lf))
        y1 = numpy.array(self.speed_lf)
        y2 = numpy.array(self.speed_rf)
        y3 = numpy.array(self.speed_lb)
        y4 = numpy.array(self.speed_rb)
        plt.plot(x, y1, ":b", x, y2, ":g", x, y3, ":y", x, y4, ":r")
        plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
        plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

        plt.xlabel("时间", size=12)
        plt.ylabel("速度", size=12)

        plt.title("speed")
        plt.legend(["左前轮", "右前轮", "左后轮", "右后轮"], shadow=True, fancybox="blue")

        plt.show()

    def plot_encoder(self):
        y1 = numpy.array(self.encoder_lf)
        y2 = numpy.array(self.encoder_rf)
        y3 = numpy.array(self.encoder_lb)
        y4 = numpy.array(self.encoder_rb)
        # pic1 = plt.plot(y1, ":b", label=u"左前轮")
        pic2 = plt.plot(y2, ":g", label=u"右前轮")
        # pic3 = plt.plot(y3, ":y", label=u"左后轮")
        pic4 = plt.plot(y4, ":r", label=u"右后轮")
        plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
        plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

        plt.xlabel("时间", size=12)
        plt.ylabel("编码器", size=12)

        plt.title("encoder")
        plt.legend(shadow=True, fancybox="blue")

        plt.show()


if __name__ == "__main__":
    m = Mat()
